const mongoose = require('mongoose')
const { timeFormat } = require('../utils/format')
/**
 * 当前登录人
 * 登录手机号
 * 昵称
 * 联系人
 * 联系人手机号
 * 联系人省市区
 * 联系人详细地址
 * 上门日期
 * 上门时间
 * 图片详情
 * 备注信息
 * 维修类型
 */

// 维修订单
const ServeSchema = new mongoose.Schema(
  {
    userPhoneNo: {
      type: String,
      required: true,
    },
    orderNo: {
      type: Number,
      required: true,
    },
    nickName: {
      type: String,
    },
    name: {
      type: String,
    },
    telphone: {
      type: String,
    },
    city: {
      type: Object,
    },
    address: {
      type: String,
    },

    files: {
      type: Object,
    },
    notes: {
      type: String,
    },
    preDate: {
      type: Object,
    },

    price: {
      type: String,
    },
    statusType: {
      type: Number,
    },
    type: {
      type: String,
    },
  },
  {
    timestamps: true,
  }
)

// 产品 订单
const ProductSchema = new mongoose.Schema(
  {
    userPhoneNo: {
      type: String,
      required: true,
    },
    nickName: {
      type: String,
    },

    productNo: {
      type: Number,
      required: true,
    },
    productName: {
      type: String,
    },
    name: {
      type: String,
    },
    address: {
      type: String,
    },
    message: {
      type: String,
    },
    price: {
      type: String,
    },
  },
  {
    timestamps: true,
  }
)

// 自定义日期格式化函数
function formatDate(date) {
  if (!date) return null
  return timeFormat(date)
}
ProductSchema.pre('save', function (next) {
  formatDate(this.createdAt)
  formatDate(this.updatedAt)
  next()
})

ServeSchema.pre('save', function (next) {
  formatDate(this.createdAt)
  formatDate(this.updatedAt)
  next()
})

module.exports = {
  WechatOrder: mongoose.model('WechatOrder', ServeSchema),
  ProductOrder: mongoose.model('ProductOrder', ProductSchema),
}
